# coding=utf-8

"""
这个程序是爬取汽车之家结果

1、 根据心中的价格，获取车辆信息 ，并且输出到csv中，可以参考

"""

from bs4 import BeautifulSoup
import requests
import csv

datas = [
        ['车名','级别','结构','发动机','最低价','最高价','评分']
    ]

def get_html(req_url):
    """获取html 返回的内容"""
    response = requests.get(req_url)
    if response.status_code == 200:
        return response.text
    else :
        return None


def format_html(html_content):
    """格式化html"""
    bs = BeautifulSoup(html_content,"lxml")
    return bs


def get_result_cars(bs_html):
    """ 根据查询结果,返回 tag list """
    grid_html = bs_html.select(".content")[0].select(".row")[0].select(".row")[3]
    result_content_list = grid_html.select(".tab-content-item")[0].select(".list-cont")
    return result_content_list



def write_to_data(car_tag_list):
    """写入到csv中"""

    for car_tag in car_tag_list:

        try:

            title = car_tag.select(".list-cont-main .main-title a")[0].text

            print("正在爬取车型{0}.....".format(title))

            left_li_list = car_tag.select(".list-cont-main .main-lever-left .lever-ul li")
            level = left_li_list[0].select("span")[0].text  # 级别，紧凑等

            if len(left_li_list[1].select("a")) > 0:
                contract = left_li_list[1].select("a")[0].text  # 车身结构，几箱车
            else:
                contract = "电动车续航" + left_li_list[1].text  # 车身结构，几箱车

            eng = left_li_list[2].select("span")[0].text  # 发动机 ，排量

            right_div = car_tag.select(".list-cont-main .main-lever-right")[0]
            price = right_div.select(".lever-price span")[0].text  # 价格
            if price.find("-") >= 0:
                minp = price.replace("万", "").split("-")[0]  # 最低值
                maxp = price.replace("万", "").split("-")[1]  # 最高值
            else:
                minp = 0
                maxp = 0

            # 用户评分
            score = right_div.select(".score-cont")[0].select(".score-number")[0].text

            data_str = title + "," + level + "," + contract + "," + eng + "," + str(minp) + "," + str(maxp) + "," + str(
                score)
            datas.append(data_str.split(","))
        except:
            continue

def write_to_csv():
    """ 写入csv """

    # newline 这个参数是解决每行多出一个空行
    with open('car.csv', 'a', encoding="utf-8", newline='') as f:
        writer = csv.writer(f, dialect='excel')
        for d in datas:
            writer.writerow(d)



if __name__ == "__main__":
    minPirce = 5
    maxPirce = 15


    for page in range(1,36):

        print("==============")

        print("当前页码:"+str(page))

        url = "https://car.autohome.com.cn/price/list-" + str(minPirce) + "_" + str(
            maxPirce) + "-0-0-0-0-0-0-0-0-0-0-0-0-0-0-"+str(page)+".html"

        # 获取内容
        content = get_html(url)

        # 格式化内容
        html = format_html(content)

        # 获取所有查询的结果
        cars_list = get_result_cars(html)

        # 输出到csv中
        write_to_data(cars_list)

        write_to_csv()

        print("==============")





